﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using kwzx.Data;
using kwzx.Common;

namespace kwzx
{
    public partial class KGS_fk : Form
    {
        DataSet1TableAdapters.K_DeviceIssueTableAdapter k_deviceIssue = new DataSet1TableAdapters.K_DeviceIssueTableAdapter();
        DataSet1TableAdapters.T_BMTableAdapter bm = new DataSet1TableAdapters.T_BMTableAdapter();
        DataSet1TableAdapters.T_GROUPTableAdapter group = new DataSet1TableAdapters.T_GROUPTableAdapter();
        DataSet1TableAdapters.V_jg_bmTableAdapter vjgbm = new DataSet1TableAdapters.V_jg_bmTableAdapter();
        DataSet1TableAdapters.K_CardTableAdapter k_card = new DataSet1TableAdapters.K_CardTableAdapter();

        string fkbk = "";
        int k_gs_id = 0;
        public KGS_fk(string fkbk1)
        {
            fkbk = fkbk1;
            InitializeComponent();
        }

        private void fk_Load(object sender, EventArgs e)
        {
            DataTable group_tb = group.GetDataBy_sjjg(0);
            for (int g = 0; g < group_tb.Rows.Count; g++)
            {
                TreeNode tn = new TreeNode();
                tn = treeView1.Nodes.Add(group_tb.Rows[g]["JGNAME"].ToString());
                tn.Text = group_tb.Rows[g]["jc"].ToString();
                tn.Tag = -1;
                tn.ToolTipText = "jg";
                tn.ImageIndex = 0;
                tn.SelectedImageIndex = 0;
              //  tn.Checked = true;
               //addJGTree(tn.Tag.ToString(), tn);
                addBMTree(group_tb.Rows[g]["GRPUP_ID"].ToString(), "0", tn);
            }
            //外协单位
            TreeNode tn1 = new TreeNode();
            tn1 = treeView1.Nodes.Add("外协单位");
            tn1.Text = "外协单位";
            tn1.Tag = "0";
            tn1.ToolTipText = "jg";
            tn1.ImageIndex = 0;
            tn1.SelectedImageIndex = 0;
            tn1.Checked = true;
            addJGTree(group_tb.Rows[0]["GRPUP_ID"].ToString(), tn1);
            treeView1.Nodes[0].ExpandAll();
            treeView1.SelectedNode = treeView1.Nodes[0].NextVisibleNode;
           
           // treeView1.ExpandAll();
        }
    
     

        //private void FillTView()
        //{
        //    //根
        //    treeView1.BeginUpdate();
        //    treeView1.Nodes.Clear();
        //    TreeNode rootNode = new TreeNode("公司名称");
        //    treeView1.Nodes.Add(rootNode);

        //    string oldDept = "公司名称";
        //    string curDept = "";

        //    TreeNode parentNode = rootNode;
        //    TreeNode childNode;

        //    DataTable dt = vjgbm.GetData();
        //    //DataTable dt = GetDataTable("SELECT 总部门, 分部门 FROM TableName ORDER BY 总部门, 分部门");
        //    for (int j = 0; j < dt.Rows.Count; j++)
        //    {
        //        curDept = dt.Rows[j][0].ToString();
        //        if (curDept != oldDept)
        //        {
        //            parentNode = new TreeNode();
        //            parentNode.Text = curDept;
        //            rootNode.Nodes.Add(parentNode);
        //            oldDept = curDept;
        //        }

        //        childNode = new TreeNode();
        //        childNode.Text = dt.Rows[j][1].ToString();
        //        parentNode.Nodes.Add(childNode);
        //    }
        //    treeView1.EndUpdate();
        //}


        /// <summary>
        /// 递归加载addJGTree列表
        /// </summary>
        /// <param name="fatherid">节点的fatherid</param>
        /// <param name="pnode">该节点</param>
        private void addJGTree(string fatherid, TreeNode pnode)
        {
            DataTable dt = group.GetDataBy_sjjg(int.Parse(fatherid));
            DataView dvTree = new DataView(dt);
            dvTree.RowFilter = "[SJJG] = " + fatherid;
            foreach (DataRowView drv in dvTree)
            {
                TreeNode node = new TreeNode();
                node.ImageIndex = 1;
                node.SelectedImageIndex = 3;
                if (pnode == null)
                {
                    node.Text = drv["jc"].ToString();
                    node.Tag = drv["GRPUP_ID"].ToString();    //作为节点标识符
                    node.ToolTipText = "jg";
                    treeView1.Nodes.Add(node);
                    addJGTree(drv["GRPUP_ID"].ToString(), node);//递归  该节点加载子节点
                }
                else
                {
                    node.Text = drv["jc"].ToString();
                    node.Tag = drv["GRPUP_ID"].ToString();//作为节点标识符
                    node.ToolTipText = "jg";
                    pnode.Nodes.Add(node);
                    addJGTree(drv["GRPUP_ID"].ToString(), node);//递归  该节点加载子节点

                }
            }
        }

        /// <summary>
        /// 递归加载addBMTree列表
        /// </summary>
        /// <param name="fatherid">节点的fatherid</param>
        /// <param name="pnode">该节点</param>
        private void addBMTree(string jgid, string fatherid, TreeNode pnode)
        {
            DataTable dt = bm.GetDataBy_grpup_id(int.Parse(jgid));
            DataView dvTree = new DataView(dt);
            dvTree.RowFilter = "[SJBM] = " + fatherid;
            foreach (DataRowView drv in dvTree)
            {
                TreeNode node = new TreeNode();
                node.ImageIndex = 1;
                node.SelectedImageIndex = 3;
                if (pnode == null)
                {
                    node.Text = drv["BMMC"].ToString();
                    node.Tag = drv["DEPT_ID"].ToString();    //作为节点标识符
                    node.ToolTipText = "bm";
                    treeView1.Nodes.Add(node);
                    addBMTree(jgid, drv["DEPT_ID"].ToString(), node);//递归  该节点加载子节点
                }
                else
                {
                    node.Text = drv["BMMC"].ToString();
                    node.Tag = drv["DEPT_ID"].ToString();//作为节点标识符
                    node.ToolTipText = "bm";
                    pnode.Nodes.Add(node);
                    addBMTree(jgid, drv["DEPT_ID"].ToString(), node);//递归  该节点加载子节点
                }
            }
        }

        /// <summary>
        /// 树选中一结点时，在右侧的GridView中关联显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                int id = int.Parse(e.Node.Tag.ToString());
                string tooltiptext = e.Node.ToolTipText;

                if (tooltiptext.Equals("jg"))
                {
                    this.v_user_cardTableAdapter.FillBy_Allchild(this.dataSet1.V_user_card, int.Parse(treeView1.SelectedNode.Tag.ToString()));
                }
                else if (tooltiptext.Equals("bm"))
                {
                    this.v_user_cardTableAdapter.FillBy_DEPT_id(this.dataSet1.V_user_card, int.Parse(treeView1.SelectedNode.Tag.ToString()));
                }
            }
           // button4_Click(sender, e);
            string sql = " 1=1 ";

            if (this.radioButton1.Checked == true)
            {
                sql += "and kzt='发卡'";
            }
            else
            {
                sql += "and kzt='挂失'";
            }

           vusercardBindingSource.Filter = sql;
            //user_cardBindingSource_PositionChanged(sender, e);
           if (vusercardBindingSource.Count < 1)
           {
               pictureBox1.Image = global::kwzx.Properties.Resources.无图片;
           }
        }

        /// <summary>
        /// 读取卡号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 级联人员表及卡表信息显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void user_cardBindingSource_PositionChanged(object sender, EventArgs e)
        {
            if (vusercardBindingSource.Current != null)
            {
                string users_id = ((DataRowView)vusercardBindingSource.Current)["user_id"].ToString();
                string users_id9 = ((DataRowView)vusercardBindingSource.Current)["user_id"].ToString();
                string Imgpath = ((DataRowView)vusercardBindingSource.Current)["zp"].ToString(); ;
                if (!string.IsNullOrWhiteSpace(Imgpath))
                {
                    int x = Imgpath.IndexOf("ftp/") + 4;
                    string userImg = Imgpath.Substring(x, Imgpath.Length - x);   //ftp下载路径
                    // 路径为:DBFZSPersonImg/李瑞明131684197012105231.jpg
                    int userImginfo = userImg.IndexOf("/") + 1;
                    string spath = SPicUtils.path + "\\";
                    string zpname = users_id9 + ".dat";
                    string kkzp = FileTran.DownloadFile(spath, zpname, userImg);
                    // string imgname = userImg.Substring(userImginfo, (userImg.Length - userImginfo));
                    this.pictureBox1.Image = SPicUtils.PicConvert(kkzp);
                }
                else
                {
                    pictureBox1.Image = global::kwzx.Properties.Resources.无图片;
                }

                rybhTextBox.Text = ((DataRowView)vusercardBindingSource.Current)["rybh"].ToString();
                zSXMTextBox.Text = ((DataRowView)vusercardBindingSource.Current)["zsxm"].ToString();
                dEPT_idTextBox.Text = ((DataRowView)vusercardBindingSource.Current)["jc"].ToString();
                if (((DataRowView)vusercardBindingSource.Current)["uby"].ToString() == "-1")
                {
                    dEPT_idTextBox.Text = ((DataRowView)vusercardBindingSource.Current)["bmmc"].ToString();

                }
                dEPT_idTextBox.Text = dEPT_idTextBox.Text.Replace("(党委组织部)", "").Replace("(纪委办公室)", "");
                uBYTextBox.Text = ((DataRowView)vusercardBindingSource.Current)["gsbm"].ToString();
                gzTextBox.Text = ((DataRowView)vusercardBindingSource.Current)["gzmc"].ToString();
                gzTextBox.Text = gzTextBox.Text.Replace("（文秘、司机、厨师、保安、保洁、绿化）", "");
                xBTextBox.Text = ((DataRowView)vusercardBindingSource.Current)["xb"].ToString();
                zWTextBox.Text = ((DataRowView)vusercardBindingSource.Current)["zw"].ToString();
                dHTextBox.Text = ((DataRowView)vusercardBindingSource.Current)["dh"].ToString();
                sfzhTextBox.Text = ((DataRowView)vusercardBindingSource.Current)["sfzh"].ToString();

                string CardID = ((DataRowView)vusercardBindingSource.Current)["CardID"].ToString();

                this.k_CarddataTableAdapter.FillBy_user_id(this.dataSet1.K_Carddata, Convert.ToInt32(users_id));
            }
            else
            {
                rybhTextBox.Text = "";
                zSXMTextBox.Text = "";
                dEPT_idTextBox.Text = "";
                uBYTextBox.Text = "";
                gzTextBox.Text = "";
                xBTextBox.Text = "";
                zWTextBox.Text = "";
                dHTextBox.Text = "";
                sfzhTextBox.Text = "";             
            }
        }
 
        /// <summary>
        /// 搜索操作(查询功能)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            string sql = " 1=1 ";
            if (this.textBox1.Text.ToString() != "")
            {
                sql += " and ZSXM like '%" + this.textBox1.Text.ToString() + "%'";
            }
            if (this.textBox2.Text.ToString() != "")
            {
                sql += "and sfzh like '%" + this.textBox2.Text.ToString() + "%'";
            }
            if (this.radioButton1.Checked == true)
            {
                sql += "and kzt='发卡'";
            }
            else
            {
                sql += "and kzt='挂失'";
            }

            if (this.textBox3.Text.ToString() != "")
            {
                sql += " and kh like '%" + this.textBox3.Text.ToString() + "%'";
            }

            vusercardBindingSource.Filter = sql;
          
            user_cardBindingSource_PositionChanged(sender, e);
            this.user_cardDataGridView.Refresh();
            if (vusercardBindingSource.Count < 1)
            {
                pictureBox1.Image = global::kwzx.Properties.Resources.无图片;
            }
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            string sql = " 1=1 ";
            RadioButton rb = sender as RadioButton;
            if (rb.Name == "radioButton1")
            {
                sql += "and kzt='发卡'";
            }
            else if (rb.Name == "radioButton2")
            {
                sql += "and kzt='挂失'";
            }
            vusercardBindingSource.Filter = sql;
            user_cardBindingSource_PositionChanged(sender, e);
            this.user_cardDataGridView.Refresh();
            if (vusercardBindingSource.Count < 1)
            {
                pictureBox1.Image = global::kwzx.Properties.Resources.无图片;
            }
        }

        private void fkbutton_Click(object sender, EventArgs e)
        {

            if (radioButton2.Checked)
            {
                MessageBox.Show("已挂失, 不能重复挂失！");
                return;
            }
            string user_id = ((DataRowView)vusercardBindingSource.Current)["user_id"].ToString();
          if (user_id == "")
            {
                MessageBox.Show("请先选择人员！");
                return;
            }

             string ZSXM = ((DataRowView)vusercardBindingSource.Current)["ZSXM"].ToString();
            DialogResult dr = MessageBox.Show(" 确定要挂失【" + ZSXM + "】的卡？", "取消提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                int CardID = int.Parse(((DataRowView)vusercardBindingSource.Current)["CardID"].ToString());

                DataSet1TableAdapters.K_CardTableAdapter kc = new DataSet1TableAdapters.K_CardTableAdapter();
                kc.UpdateQuery_kzt("挂失", CardID);



                try
                {

                    DataSet1TableAdapters.K_CarddataTableAdapter kcad = new DataSet1TableAdapters.K_CarddataTableAdapter();

                    string CardtypeID2 = ((DataRowView)vusercardBindingSource.Current)["sfzh"].ToString();
                    int CD_CardType = -1;
                    if (((DataRowView)vusercardBindingSource.Current)["CT_ID"].ToString() != "")
                    {
                        CD_CardType = int.Parse(((DataRowView)vusercardBindingSource.Current)["CT_ID"].ToString());
                    }
                    string kh = ((DataRowView)vusercardBindingSource.Current)["kh"].ToString();
                    string SM = sMTextBox.Text;
                    int SXH = 0;
                    DateTime czrq = DateTime.Now;
                    string kzt = fkbk;
                    string sh = "0";
                    decimal thje = 0;
                    string lrz = Utils.CurrentUser.xm;
                    int xfzt = 0;
                    DateTime zzrq = DateTime.Now;
                    int user_id1 = Convert.ToInt32(user_id);
                    kcad.Insert(user_id1, CardtypeID2, CD_CardType, kh, SM, SXH, czrq, kzt, sh, thje, lrz, xfzt, zzrq);
                    string CardID_dv = ((DataRowView)vusercardBindingSource.Current)["CardID"].ToString();
                    k_deviceIssue.InsertQuery(kh, "0", DateTime.Now, "K_Card", CardID_dv.ToString(), "挂失", "");
                    //DDS数据更新
                    string pcname_dds = Properties.Settings.Default.pcname_dds.ToString();
                    string sfzh = ((DataRowView)vusercardBindingSource.Current)["sfzh"].ToString();
                    string zSXM = ((DataRowView)vusercardBindingSource.Current)["zSXM"].ToString();
                    string BMMC = ((DataRowView)vusercardBindingSource.Current)["BMMC"].ToString();
                    string fromdate = "";
                    string to_date = "";
                    fromdate = ((DataRowView)vusercardBindingSource.Current)["fdate"].ToString();
                    to_date = ((DataRowView)vusercardBindingSource.Current)["jdate"].ToString();
                    Spread sp = new Spread();
                    sp.StartConnect();
                    sp.useradd(pcname_dds, sfzh, kh, zSXM, user_id.ToString(), BMMC, fromdate, to_date, "挂失");
                    sp.DesInit();


                    MessageBox.Show(kzt + "成功！");
                    if (this.treeView1.SelectedNode.ToolTipText.Equals("jg"))
                    {
                        this.v_user_cardTableAdapter.FillBy_Allchild(this.dataSet1.V_user_card, int.Parse(treeView1.SelectedNode.Tag.ToString()));
                    }
                    else if (treeView1.SelectedNode.ToolTipText.Equals("bm"))
                    {
                        this.v_user_cardTableAdapter.FillBy_DEPT_id(this.dataSet1.V_user_card, int.Parse(treeView1.SelectedNode.Tag.ToString()));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }        
        }

        private void gbbutton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
